﻿#pragma once

#include "Event.h"

#include <functional>
#include <unordered_map>
#include <vector>

class EventDispatcher
{
public:
  using EventCallback = std::function<void(Event &)>;

  // 订阅特定类型事件
  void Subscribe(EventType type, EventCallback callback)
  {
    m_subscribers[type].push_back(callback);
  }

  // 分发事件到所有订阅者
  void Dispatch(Event &event)
  {
    EventType type = event.GetEventType();
    auto it = m_subscribers.find(type);
    if (it != m_subscribers.end())
    {
      for (auto &callback : it->second)
      {
        callback(event);
        if (event.Handled)
          break; // 事件已处理则停止传播
      }
    }
  }

private:
  std::unordered_map<EventType, std::vector<EventCallback>> m_subscribers;
};
